#include "toast.h"
#include <QBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QEventLoop>
#include <QTimer>
#include <QPropertyAnimation>

#include <QGuiApplication>
#include <QScreen>

Toast::Toast(const QString &message, const QPoint &centerPos, const QString& imgObjName, QWidget *parent)
    : QWidget(parent)
    , message(message)
    , centerPos(centerPos)
    , imgObjName(imgObjName)
{
    setWindowFlags(Qt::FramelessWindowHint
                   | Qt::SubWindow
                   | Qt::WindowSystemMenuHint
                   | Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground);

    setupUi();
    createConnect();
}

Toast::~Toast()
{
}

void Toast::setupUi()
{
    QWidget *widget = new QWidget(this);
    widget->setObjectName("toastTabel");

    QHBoxLayout *mainLayout = new QHBoxLayout(widget);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);
    setLayout(mainLayout);

    QLabel *label = new QLabel(widget);
    label->setObjectName("14pxWhiteLabel");
    label->setText(message);

    QFont font(label->font());
    font.setPixelSize(14);
    QFontMetrics fm(font);
    int frontSize = fm.width(message);

    if(!imgObjName.isEmpty())
    {
        mainLayout->addStretch();
        QLabel *img = new QLabel(widget);
        img->setObjectName(imgObjName);
        img->setFixedSize(16, 16);
        mainLayout->addWidget(img);
        mainLayout->addSpacing(8);
        mainLayout->addWidget(label);
        mainLayout->addStretch();

        int w = std::max(64 + frontSize, 244);
        widget->setFixedSize(w, 40);
    }
    else
    {
        label->setAlignment(Qt::AlignCenter);
        mainLayout->addWidget(label);

        int w = std::max(40 + frontSize, 244);
        widget->setFixedSize(w, 40);
    }

    setGeometry(centerPos.x() - widget->width() / 2,
                centerPos.y() - widget->height() / 2,
                widget->width(),
                widget->height());
}

void Toast::createConnect()
{
}

void Toast::toast(const QString &message, const QPoint &centerPos, QWidget *parent)
{
    Toast t(message, centerPos, "", parent);

    if(parent != nullptr)
    {
        int startx = (parent->width()-t.width()) / 2;
        int starty = parent->height();
        int endy = parent->height()*3/4;

        QPropertyAnimation *animal= new QPropertyAnimation(&t,"pos");
        animal->setDuration(500);
        animal->setStartValue(QPoint(startx,starty));
        animal->setEndValue(QPoint(startx,endy));
        animal->setEasingCurve(QEasingCurve::OutCubic);
        animal->start();
    }

    t.show();


    QEventLoop eventLoop;
    QTimer::singleShot(1500, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
}

void Toast::toast(const QString &imgObjName, const QString &message, const QPoint &centerPos, QWidget *parent /*= Q_NULLPTR*/)
{
    Toast t(message, centerPos, imgObjName, parent);


    int startx = centerPos.x();
    int starty = centerPos.y()+48;
    int endy = centerPos.y();

    QPropertyAnimation *animal= new QPropertyAnimation(&t,"pos");
    animal->setDuration(500);
    animal->setStartValue(QPoint(startx,starty));
    animal->setEndValue(QPoint(startx,endy));
    animal->setEasingCurve(QEasingCurve::OutCubic);
    animal->start();

    t.show();



    QEventLoop eventLoop;
    QTimer::singleShot(1500, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
}

void Toast::toast(Type type, const QString &message, QWidget *parent)
{
    // 获取屏幕中心点
    QPoint  centerPos = QGuiApplication::primaryScreen()->geometry().center();

    if(parent){
        centerPos.setX( parent->x() + parent->width() / 2);
        centerPos.setY(parent->y() + parent->height() / 2);
    }

    QString imgObjName;
    switch (type) {
    case Succ:
        imgObjName = "succImg";
        break;
    case Warn:
        imgObjName = "warningImg";
        break;
    default:
        imgObjName = "infoImg";
    }

    Toast::toast(imgObjName, message, centerPos, parent);
}

